package com.yswl.pri.home.vedio;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yswl.pri.R;
import com.yswl.pri.common.ToolbarActivity;
import com.yswl.pri.guide.bean.Musicbean;
import com.yswl.pri.picture.SpacesItemDecoration;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import yswl.com.klibrary.base.BaseRecyclerAdapter;
import yswl.com.klibrary.base.BaseViewHolder;
import yswl.com.klibrary.http.CallBack.HttpCallback;
import yswl.com.klibrary.http.HttpClientProxy2;
import yswl.com.klibrary.util.EmptyRecyclerView;
import yswl.com.klibrary.util.L;

/**
 * 搜索种子列表
 */
public class MusicSearchListActivity extends ToolbarActivity implements HttpCallback<String> {

    private static final String TAG = "MusicSearchListActivity";
    private static final int reId = 1;
    private static final String DEF_HOST = "http://gg.sln3.xyz/";

    public static void JumpAct(Context context, String search) {
        Intent intent = new Intent(context, MusicSearchListActivity.class);
        intent.putExtra("search", search);
        context.startActivity(intent);
    }

    RefreshLayout refreshLayout;
    MusicSearchListAdapter movieSearchListAdapter;
    public String key;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_search_list);
        key = getIntent().getStringExtra("search");
        setToolbarTitle(key + "  搜索 ");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                loadResource(key);
            }
        });
        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        SpacesItemDecoration decoration = new SpacesItemDecoration(5);
        recyclerView.addItemDecoration(decoration);

        movieSearchListAdapter = new MusicSearchListAdapter(this, null, R.layout.activity_music_search_list_item);
        EmptyRecyclerView emptyView = new EmptyRecyclerView(this);
        emptyView.setNoticeAndIcon("没有找到哟", "暂无种子信息嘞", -1);
        movieSearchListAdapter.setEmptyView(emptyView);
        recyclerView.setAdapter(movieSearchListAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        refreshLayout.autoRefresh();
        refreshLayout.setEnableLoadMore(false);

    }


    void loadResource(String key) {
        String url;
        url = "http://gg.sln3.xyz/search/" + key;
        HttpClientProxy2.getInstance().getAsyn(url, reId, null, this);
    }


    @Override
    public void onSucceed(int requestId, String result) {
        refreshLayout.finishRefresh();
        if (requestId == reId) {
            L.d(TAG, " onSucceed : " + result);
            Document document = Jsoup.parse(result);
            String title = document.title();
            L.e(TAG, " title : " + title);

            Elements resous = document.select(".item-title a");
            if (resous == null || resous.size() == 0) return;

            List<Musicbean> list = new ArrayList<>();
            for (int i = 0; i < resous.size(); i++) {
                Musicbean musicbean = new Musicbean();
                String text = resous.get(0).text();
                String urll = resous.get(0).attr("href");
                musicbean.title = text;
                musicbean.link = DEF_HOST + urll;
                list.add(musicbean);
            }
            movieSearchListAdapter.setList(list);
        }

    }

    @Override
    public void onFail(int requestId, String errorMsg) {
        L.e(TAG, " onFail : " + errorMsg);
        refreshLayout.finishRefresh();
    }


    public class MusicSearchListAdapter extends BaseRecyclerAdapter<Musicbean> {
        int mPosition = -1;

        public MusicSearchListAdapter(Context mContext, List<Musicbean> mList, int itemLayoutId) {
            super(mContext, mList, itemLayoutId);
        }

        @Override
        public void onBind(BaseViewHolder holder, final Musicbean imag, final int position) {
            final TextView textView = holder.findById(R.id.name);
            textView.setText(imag.title);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MusicDetailActivity.JumpAct(MusicSearchListActivity.this, key, "https://www.sobt8.com" + imag.link);
                    mPosition = position;
                    textView.setTextColor(getResources().getColor(R.color.colorAccent));
                }
            });
            if (position == mPosition) {
                textView.setTextColor(getResources().getColor(R.color.colorAccent));
            } else {
                textView.setTextColor(getResources().getColor(R.color.text));
            }
        }
    }
}
